package com.thenie.chat.controller;

import com.thenie.chat.pojo.User;
import com.thenie.chat.service.IAddRequestService;
import com.thenie.chat.service.IFriendService;
import com.thenie.chat.validator.CurrentUser;
import com.thenie.chat.vo.FriendRequestVo;
import com.thenie.chat.vo.UserInfoVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import static com.thenie.chat.controller.DialogWorldSocketHandler.WORLD_SESSION_MAP;


/**
 * @Description 跳转页面
 * @Author thenie
 * @Data 2022/5/17 22:03
 */
@Controller
public class IndexController {
    @Autowired
    private IFriendService friendService;
    @Autowired
    private IAddRequestService addRequestService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 跳转主页
     * @param model
     * @param user
     * @return
     */
    @GetMapping(value = {"/index","index.html","/"})
    public String indexHTML(Model model, @CurrentUser User user){
        //保存 用户的个人信息
        model.addAttribute("user", user);

        //保存 用户的好友别表
        List<UserInfoVo> myFriendList = friendService.findMyFriendListOrderByOnLine(user.getId());
        model.addAttribute("friendList",myFriendList);


        //保存 好友列表和下标对应关系
        HashMap<Long, Integer> idIdxMap = new HashMap<>();
        for (int i = 0; i < myFriendList.size(); i++) {
            idIdxMap.put(myFriendList.get(i).getId(),i);
        }
        model.addAttribute("idIdxMap", idIdxMap);

        //保存 好友请求记录
        List<FriendRequestVo> friendRequestList = addRequestService.queryFriendRequestList(user.getId());
        model.addAttribute("friendRequestList",friendRequestList);

        return "index";
    }

    /**
     * 跳转登录页面
     * @return
     */
    @GetMapping("/login")
    public String toLogin(){
        return "login";
    }

    /**
     * 跳转注册页面
     * @return
     */
    @GetMapping("/register")
    public String register(){
        return "register";
    }


    /**
     * 跳转世界聊天窗口
     * @param model
     * @param user
     * @return
     */
    @GetMapping("/world-window")
    public String worldWindow(Model model, @CurrentUser User user){
        if (WORLD_SESSION_MAP.containsKey(user.getId().toString())) {
            model.addAttribute("warn","不可以同时进入多个【世界之窗】界面哦~");
            return "error/error-window";
        }
        //查询当前世界窗口在线用户信息
        List<Object> worldUserList =new ArrayList<>();
        Set<String> keys = redisTemplate.keys("worldUser:*");

        if (keys!=null){
             worldUserList = redisTemplate.opsForValue().multiGet(keys);
        }

        model.addAttribute("user",user);
        model.addAttribute("worldUserList",worldUserList);

        return "world-window";
    }

    /**
     * 游戏模块跳转
     */
    @GetMapping("/game/racing")
    public String gameSaiChe(){
        return "game-racing";
    }
    @GetMapping("/game/poker")
    public String gamePoker(){
        return "game-poker";
    }
    @GetMapping("/game/cube")
    public String gameCube(){
        return "game-cube";
    }
    @GetMapping("/game/xiaoxiaole")
    public String gameXxLe(){
        return "game-xiaoxiaole";
    }
    @GetMapping("/game/huochairen")
    public String gameHcRen(){
        return "game-huochairen";
    }
    @GetMapping("/game/paoshou")
    public String gamePaoSho(){
        return "game-paoshou";
    }

}
